<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary">添加分类</el-button>
      <!-- 表格数据 -->
      <tree-table
        :data="categories"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template slot-scope="scope" slot="isOk">
          <i
            class="el-icon-success"
            style="color: lightgreen"
            v-if="scope.row.cat_deleted == 0"
          ></i>
          <i class="el-icon-error" v-if="scope.row.cat_deleted == 1"></i>
        </template>
        <template slot-scope="scope" slot="sort">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag type="success" v-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag
            type="warning"
            v-if="
              scope.row.cat_level == 2 ||
              scope.row.cat_level == null ||
              scope.row.cat_level == 3
            "
            >三级</el-tag
          >
        </template>
        <template slot-scope="scope" slot="opt">
          <el-button size="mini" type="primary">
            <i class="el-icon-edit">编辑</i>
          </el-button>
          <el-button size="mini" type="danger" @click="del(scope.row.cat_id)">
            <i class="el-icon-delete">删除</i>
          </el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getCategoriesAPI } from '@/api/categories'
import { delGoods } from '@/api/goods'
export default {
  name: '',
  data () {
    return {
      obj: {
        type: 3,
        pagenum: 1,
        pagesize: 3
      },
      categories: [],
      total: 0,
      type: '',
      level: '一级',
      //columns(设置表格中列配置信息)
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        },
      ],
    }
  },
  created () {
    this.getCategories()
  },
  computed: {
  },
  methods: {
    async getCategories () {
      const res = await getCategoriesAPI(this.obj)
      console.log(res);
      this.categories = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pagesize = val
      this.getCategories()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.pagenum = val
      this.getCategories()
    },
    del (id) {
      console.log(id);
    }
  }
}
</script>
<style lang='less'  scoped>
.success {
  padding: 2px;
}
</style>
